package com.pk.platform.business.charge.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.charge.dao.IChargeIndexDao;
import com.pk.platform.business.charge.vo.ChargeIndexVO;
import com.pk.platform.business.core.dao.impl.GenericDaoImpl;
import com.pk.platform.domain.charge.ChargeIndex;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class ChargeIndexDaoImpl extends GenericDaoImpl implements
		IChargeIndexDao {

	public ListPage queryChargeIndex(ChargeIndexVO civo, Pager pager) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from charge_index ci where 1 = 1");
		List<Object> param = new ArrayList<Object>();
		if(StringConverter.isNotNull(civo.getCi().getIndexName()))
		{
			sql.append(" and ci.indexName like ?");
			param.add("%" + civo.getCi().getIndexName() + "%");
		}
		if(StringConverter.isNotNull(civo.getCi().getDescription()))
		{
			sql.append(" and ci.description like ?");
			param.add("%" + civo.getCi().getDescription() + "%");
		}
		if(civo.getAmountStart() != null)
		{
			sql.append(" and ci.amount >= ?");
			param.add(civo.getAmountStart());
		}
		if(civo.getAmountEnd() != null)
		{
			sql.append(" and ci.amount <= ?");
			param.add(civo.getAmountEnd());
		}
		if(civo.getCi().getIsDel() != null)
		{
			sql.append(" and ci.isDel = ?");
			param.add(civo.getCi().getIsDel());
		}
		if(StringConverter.isNotNull(civo.getCi().getIndexType()))
		{
			sql.append(" and ci.indexType = ?");
			param.add(civo.getCi().getIndexType());
		}
		sql.append(" order by ci.sort");
		return this.getListPage(sql.toString(), param.toArray(), pager);
	}
	
	public List<ChargeIndex> queryChargeIndexList()
	{
		String hql = "from ChargeIndex ci where isDel = 0 order by ci.sort";
		return this.getHibernateTemplate().find(hql);
	}
	
	public List<ChargeIndex> queryAllChargeIndexList()
	{
		String hql = "from ChargeIndex ci order by ci.sort";
		return this.getHibernateTemplate().find(hql);
	}
	
	//修改时调出的模板指标
	public List<Map<String, Object>> queryHasSetChargeIndexList(String tempId)
	{
		StringBuffer sql = new StringBuffer();
		sql.append("select max(t.id) id, max(t.tempId) tempId, t.indexId, max(t.sort) sort, max(t.amount) amount, count(t.indexId) as quantity");
		sql.append(" from temp_index_ref t where t.tempId = ? group by t.indexId, t.team order by sort");
		return this.getSjt().queryForList(sql.toString(), new Object[]{ tempId});
	}
	
	public boolean existChargeIndexForUpdate(String indexName, String id)
	{
		String sql = "select count(id) from charge_index where indexName = ? and id <> ?";
		int count = this.getSjt().queryForInt(sql, indexName, id);
		if(count > 0)
			return true;
		return false;
	}

}
